package com.example.demo;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.util.Collections;
import java.util.Map;

/**
 * Created by luke on 18/2/11.
 */
public class EsProcess {
    public static void main(String arg[]) throws Exception{

        //low level rest client
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")).build();

        Map<String, String> params = Collections.singletonMap("pretty", "true");
        String jsonString = "{\n" +
                "    \"query\": {\n" +
                "        \"query_string\" : {\n" +
                "            \"default_field\" : \"book\",\n" +
                "            \"query\" : \"传道书\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest("GET", "/_search",params,entity);

        System.out.println(response);

        restClient.close();


        // hive level rest client
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("localhost", 9200, "http"),
//                        new HttpHost("localhost", 9201, "http")));
//
//        GetRequest getRequest = new GetRequest(
//                "bible",
//                "zhenyan",
//                "ksB-gmEBVuZsHV55E__d");
//
//        GetResponse getResponse = client.get(getRequest);
//
//        System.out.println(getResponse);
//
//        client.close();
    }
}
